package cn.hpclub.server.service;

import com.jfinal.plugin.activerecord.Db;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.Business;
import cn.hpclub.server.model.Package;
import cn.hpclub.server.util.ResultCode;

public class BusinessService{
    public static final BusinessService service = new BusinessService();
    
    public Business findBusinessByCode(String code){
        return Business.dao.findFirst("SELECT * from business where businessCode=?", code);
    }
    
    public void updateCodeAndName(String code,String name,String Id){
        //熊诗言:更新的时候同时更新business
        Db.update("update " + Constant.Table.BUSIENSS + " set " + Business.BUSINESS_CODE + "=?,"
                + Business.BUSINESS_NAME + "=? where " + Business.ID + "=?",
                  new Object[] { code,name,Id});
    }
    
    public void saveBusinessByCodeAndName(String code,String name){
        //熊诗言:新增的时候同时新增business
        Db.update("insert into " + Constant.Table.BUSIENSS + "(" + Business.BUSINESS_CODE + "," + Business.BUSINESS_NAME  + ") value(?,?)",
                  new Object[] {code , name});
    }
    
    public boolean deleteByCodes(String[] codes){
        boolean result = true;
        if(codes.length > 0){
            StringBuffer sb = new StringBuffer();
            sb.append("UPDATE business set is_deleted=1 WHERE businessCode IN(");
            for(int i = 0; i < codes.length; i++){
                if(i == codes.length - 1){
                    sb.append("?");
                } else{
                    sb.append("?,");
                }
            }
            sb.append(")");
            int updateCount = Db.update(sb.toString(), (Object[])codes);
            if(updateCount > 0){
                result = true;
            } else{
                result = false;
            }
        }
        return result;
    }
}
